<?php

/**
 * principal actions.
 *
 * @package    paintball
 * @subpackage principal
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
require_once "includes/DB.class.php";
require_once("includes/class.phpmailer.php");
require_once("includes/class.smtp.php");
require_once("includes/Email.class.php");

class principalActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */

    
  public function executeMain(sfWebRequest $request)
  {

        try { 
            $db = DB::Instance();
            $data = $db->query("SELECT * FROM noticias limit 3");

            $this->noticias = $data->fetchAll();
            
            //print_r($this->noticias);
            
        } catch(PDOException $e) {
                $this->error = $e->getMessage();
        }
        
        
  }
    public function executeAbout(sfWebRequest $request){ 
  }
  
  public function executeBackend(sfWebRequest $request)
  { 
  }
  
  public function executeGallery(sfWebRequest $request)
  { 
  }
  
  
  public function executeLogin(sfWebRequest $request)
  { 

     $crlogin = $request->getParameter("login"); 
     $crPassword = $request->getParameter("password"); 
      
     $username = "root";
     $password = "admin";
     
     try { 
          
        $this->conn = new PDO('mysql:host=localhost;dbname=paintball', $username, $password);
        $this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); 
        
        $data = $this->conn->query('SELECT count(*) as usuarios FROM usuarios where nombre = ' . $this->conn->quote( $crlogin ) . ' && password = ' . $this->conn->quote( $crPassword ) );     

        $row = $data->fetch();
        
        if($row["usuarios"] == "1"){
            $this->getUser()->setAttribute("logged", true);
            $this->redirect('backend/main');

        } else {
            $this->getUser()->setAttribute("logged", false);

            $this->redirect('backend/main');
        }
          
     } catch(PDOException   $e) {
            echo "Error";
            $this->error = $e->getMessage();
    }
     
     
  }
  
  public function executeError(sfWebRequest $request){ 

    }
  
  public function executeContact(sfWebRequest $request){ 
      
     $this->nombre = $request->getParameter("nombre"); 
     $this->correo = $request->getParameter("correo"); 
     $this->tel = $request->getParameter("tel"); 
     $this->comentario = $request->getParameter("comentario"); 
      
     try { 
        $email = new Email();
        $subject = "Contactenos - Paintball";
        $Name = "$this->nombre";
        $message = "<p>Cotactenos:</p>
                        <p><table>
                            <tr>
                                <td>Nombre</td><td>$Name</td>
                            </tr>
                            <tr>
                                <td>Telefono Contacto</td>$this->tel<td></td>
                            </tr>    
                            <tr>
                                <td>Correo Contacto</td>$this->correo<td></td>
                            </tr>   
                            <tr>
                                <td>comentario:</td>$this->comentario<td></td>
                            </tr>    
                        </table></p>";
        
        $email->send( "Solicitud " . $subject, $message, "paintballwarriorscostarica@gmail.com", "Sistema Reservas" );
     
        }catch(PDOException $e){
            
        }
 
  }
  
  public function executeNews(sfWebRequest $request){ 
 
  }
  
  public function executeReservacion(sfWebRequest $request){
      $this->fecha2 = $request->getParameter("fecha"); 
      if(!isset($this->fecha2) ){
          $this->fecha2 = date('y-m-d');
      }
      $db = DB::Instance();
      $horarios =  Array('08:00am_11:00am','11:00am_02:00pm','02:00pm_05:00pm'); 
      $this->reserva2 = Array(3);
      for($i =0; $i < 3; $i++){
            $sql = "SELECT COUNT( * ) AS Reservado FROM  `reservas` WHERE fecha =  '$this->fecha2' AND hora =  '$horarios[$i]' AND estado =  'Confirmada'";
            $result = $db->query($sql);
            $reserva = $result->fetch();
            if($reserva["Reservado"] == 1){
                $this->reserva2[$i] = false;
            } else {
                $this->reserva2[$i] = true;
            }
            
      }
      //print_r($this->reservas);
  }
  
 public function executeInsertReservacion(sfWebRequest $request){
     
     $this->horario = $request->getParameter("horario"); 
     $this->nombre = $request->getParameter("nombre"); 
     $this->apellido1 = $request->getParameter("apellido1"); 
     $this->apellido2 = $request->getParameter("apellido2"); 
     $this->cantidadPersonas = $request->getParameter("cantidadPersonas"); 
     $this->email = $request->getParameter("email"); 
     $this->telefono = $request->getParameter("telefono"); 
     $this->fecha = $request->getParameter("fecha2"); 
     
     try { 
          
        $db = DB::Instance();
        
        $sql = "Insert into reservas(nombre, primer_apellido, segundo_apellido, correo, telefono, cantidad_personas, fecha, hora, estado) values( '$this->nombre', '$this->apellido1', '$this->apellido2',  '$this->email', '$this->telefono', '$this->cantidadPersonas', '$this->fecha', '$this->horario', 'Pendiente');";
        $data = $db->exec( $sql );     
        
        $email = new Email();
        $subject = "Reservacion Paintball $this->fecha  $this->horario - Pendiente Revision";
        $longName = "$this->nombre $this->apellido1 $this->apellido2";
        $message = "<p>Estimado Cliente:</p>
                        <p>Adjunto los datos de la Reservacion:</p>
                        <p><table>
                            <tr>
                                <td>Nombre</td><td>$longName</td>
                            </tr>
                            <tr>
                                <td>Fecha y Hora</td>$this->fecha  $this->horario<td></td>
                            </tr>
                            <tr>
                                <td>Personas</td>$this->cantidadPersonas<td></td>
                            </tr>  
                            <tr>
                                <td>Telefono Contacto</td>$this->telefono<td></td>
                            </tr>    
                        </table></p>
                        <p>Nuestro equipo lo estara contactando para confirmar la Reservacion</p>";
        
        $email->send( $subject, $message, $this->email, $longName );

        $email->send( "Solicitud " . $subject, $message, "paintballwarriorscostarica@gmail.com", "Sistema Reservas" );
     
        }catch(PDOException $e){
            
        }
        
 }

 public function executeMail(){

/*
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "ssl";
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 465;
        $mail->Username = "paintballwarriorscostarica@gmail.com";
        $mail->Password = "Qwerty2012";

        $mail->From = "paintballwarriorscostarica@gmail.com";
                $mail->FromName = "Paintball Warriors";
                $mail->Subject = "Reservacion Paintball - Pendiente Revision";
                $mail->AltBody = "";
                $mail->MsgHTML("<h1>Su reservacion esta a la espera de ser aprobada.</h1>");

        $mail->AddAddress("thomcr@gmail.com", "Robert Thomas");
        $mail->IsHTML(true);

        //Enviamos el correo electrónico
        if(!$mail->Send()) { 
            echo "Error: " . $mail->ErrorInfo; 
        }
        echo "sent";
 */
     $email = new Email();
     $email->send( "Reservacion Paintball - Pendiente Revision", "Su reservacion esta a la espera de ser aprobada.", "thomcr@gmail.com", "Robert Thomas" );
     
 }
   public function executeContactenosMailSend(sfWebRequest $request){ 
 
  }
}


